package com.binder.client;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.binder.ICalculateAIDL;

public class MainActivity extends Activity implements View.OnClickListener {

	private static final String TAG = "Client";

	private Button mBind;
	private Button mUnbind;
	private Button mAdd;
	private Button mMinus;
	private TextView mTextView;

	private ICalculateAIDL mCalculateAIDL;

	private boolean mIsBinded = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mBind = (Button) findViewById(R.id.btn_bind);
		mUnbind = (Button) findViewById(R.id.btn_unbind);
		mAdd = (Button) findViewById(R.id.btn_add);
		mMinus = (Button) findViewById(R.id.btn_minus);
		mTextView = (TextView) findViewById(R.id.txt_res);

		mBind.setOnClickListener(this);
		mUnbind.setOnClickListener(this);
		mAdd.setOnClickListener(this);
		mMinus.setOnClickListener(this);
	}

	@Override
	protected void onStop() {
		super.onStop();
		unbind();
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.btn_bind:
			Intent intent = new Intent();
			intent.setAction("com.binder.server.calculate");
			intent.setPackage("com.binder.server");
			boolean success = bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
			Log.e(TAG, "bindService success = " + success);
			break;

		case R.id.btn_unbind:
			unbind();
			break;

		case R.id.btn_add:
			if (mCalculateAIDL != null) {
				try {
					int res = mCalculateAIDL.add(3, 4);
					mTextView.setText(res + "");
				} catch (RemoteException e) {
					e.printStackTrace();
				}
			} else {
				Toast.makeText(this, "please rebind", Toast.LENGTH_SHORT).show();
			}
			break;

		case R.id.btn_minus:
			if (mCalculateAIDL != null) {
				try {
					int res = mCalculateAIDL.minus(9, 4);
					mTextView.setText(res + "");
				} catch (RemoteException e) {
					e.printStackTrace();
				}
			} else {
				Toast.makeText(this, "please rebind", Toast.LENGTH_SHORT).show();
			}
			break;
		}
	}

	private void unbind() {
		if (mIsBinded) {
			unbindService(mConnection);
			mIsBinded = false;
		}
	}

	private ServiceConnection mConnection = new ServiceConnection() {
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			Log.e(TAG, "onServiceConnected");
			mIsBinded = true;
			mCalculateAIDL = ICalculateAIDL.Stub.asInterface(service);
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			Log.e(TAG, "onServiceDisconnected");
			mCalculateAIDL = null;
			mIsBinded = false;
		}
	};
}